﻿using System;
using AdAggregator.Website;
using RealEstateAggregator.Models.Parsers;
using RealEstateAggregator.Models.SearchData;
using RealEstateAggregator.Utils;
using SortingDirection = AdAggregator.SearchData.SortingDirection;

namespace RealEstateAggregator.Models.Websites
{
    public class OtoDomPlWebsite : BaseWebsite<MySearchData, OtoDomPlSearchData, MyAdvertisement, OtoDomPlParser>
    {
        public OtoDomPlWebsite(OtoDomPlParser parser) : base(parser)
        {
        }

        protected override OtoDomPlSearchData MapSearchData(MySearchData searchData)
        {
            var data = new OtoDomPlSearchData
            {
                AdvertisementType = MapAdType(searchData.AdvertisementType),
                AreaMin = searchData.AreaMin.ToString(),
                AreaMax = searchData.AreaMax.ToString(),
                City = searchData.City,
                District = searchData.District,
                EstateType = MapEstateType(searchData.EstateType),
                FloorMin = MapFloor(searchData.FloorMin),
                FloorMax = MapFloor(searchData.FloorMax),
                Keywords = searchData.Keywords,
                MarketType = searchData.MarketType.ToString().ToLower(),
                OnlyWithPhoto = searchData.OnlyWithPhoto ? "y" : "n",
                Page = searchData.Page,
                PriceMin = searchData.PriceMin.ToString(),
                PriceMax = searchData.PriceMax.ToString(),
                PricePerMeterMin = searchData.PricePerMeterMin.ToString(),
                PricePerMeterMax = searchData.PricePerMeterMax.ToString(),
                RoomCountFrom = searchData.RoomCountMin.ToString(),
                RoomCountTo = searchData.RoomCountMax.ToString(),
                Street = searchData.Street,
                Voivodship = Constants.Voivodships.IndexOf(searchData.Voivodship).ToString(),
                YearBuiltMin = searchData.YearBuiltMin.ToString(),
                YearBuiltMax = searchData.YearBuiltMax.ToString(),
                OrderBy = MapOrderBy(searchData.OrderBy, searchData.SortingDirection)
            };
            return data;
        }

        private string MapOrderBy(string orderBy, SortingDirection sortingDirection)
        {
            string result;
            switch (orderBy)
            {
                case "Price":
                    {
                        result = "price";
                        break;
                    }
                case "PricePerMeter":
                    {
                        result = "price_m2";
                        break;
                    }
                case "Area":
                    {
                        result = "area";
                        break;
                    }
                case "Modified":
                    {
                        result = "activation_date";
                        break;
                    }
                default:
                    {
                        return "default";
                    }
            }
            return result + "%20" + sortingDirection;
        }

        private string MapFloor(int? floor)
        {
            if (floor == null)
                return String.Empty;
            
            if (floor == 0)
                return "ground_floor";
            
            return floor.ToString();
        }

        private string MapEstateType(EstateType type)
        {
            switch (type)
            {
                case EstateType.House:
                case EstateType.Flat:
                case EstateType.Room:
                case EstateType.Garage:
                    return type.ToString();
                case EstateType.Commercial:
                    return "CommercialProperty";
                case EstateType.Land:
                    return "Terrain";
                default:
                    return String.Empty;
            }
        }

        private string MapAdType(AdvertisementType type)
        {
            if (type == AdvertisementType.Sale)
                return "sell";

            if (type == AdvertisementType.Rent)
                return "rent";

            return String.Empty;
        }
    }
}